package com.example.recycler.webview;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebBackForwardList;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {
    private WebView mWebView;
//    String url="http://xw.qq.com/index.htm";
    private TextView mWvTitle;
    private ImageView mWvIcon;
    private ProgressBar mWvPb;
    String url="file:///android_asset/index.html";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.web_view);
        mWvTitle = (TextView) findViewById(R.id.wv_title);
        mWvIcon = (ImageView) findViewById(R.id.wv_icon);
        mWvPb = (ProgressBar) findViewById(R.id.wv_pb);

        mWebView.loadUrl(url);
        //让网页在我们应用内打开
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(url);
                return true;
            }
        });
        mWebView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                mWvPb.setProgress(newProgress);
            }

            @Override
            public void onReceivedIcon(WebView view, Bitmap icon) {
                super.onReceivedIcon(view, icon);
                mWvIcon.setImageBitmap(icon);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                mWvTitle.setText(title);
            }
        });


        //设置webView支持js  ，这个如果不设置会造成自己写的webView是个空白页面 解析不出html文件
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(new intfhtml(this),"android_html");
        //设置webView 支持缓存页面。
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        //设置App开启  载入webView时的默认加载的背景（就是刚加载时等待界面）
        //！！！！如果想设置背景图，必须先设置一个0背景颜色，否则背景图失效  两个是成对出现的。
        mWebView.setBackgroundColor(0);
        mWebView.setBackgroundResource(R.drawable.lancerprincess);
        //设置时候支持缩放
        mWebView.getSettings().setSupportZoom(true);
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
            //获取历史列表
            WebBackForwardList mWebBackForwardList=mWebView.copyBackForwardList();
            mWvTitle.setText(mWebBackForwardList.getCurrentItem().getTitle());

            return;
        }
        super.onBackPressed();
    }

}
